package com.eling.elcms.cater.service.impl;

import com.eling.elcms.basedoc.model.PaymentMethod;
import com.eling.elcms.cater.dao.ITravelAccountDao;
import com.eling.elcms.cater.model.TravelAccount;
import com.eling.elcms.cater.service.ITravelAccountService;
import com.eling.elcms.charge.model.Account;
import com.eling.elcms.charge.model.AccountFlow;
import com.eling.elcms.charge.service.IAccountFlowManager;
import com.eling.elcms.charge.service.IAccountManager;
import com.eling.elcms.core.AppContext;
import com.eling.elcms.core.security.web.ElingToken;
import com.eling.elcms.core.security.web.ElingTokenUtils;
import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.core.util.PropertyUtils;
import com.eling.elcms.member.model.Member;
import com.eling.elcms.member.service.IMemberManager;
import com.eling.elcms.system.model.AppUser;
import com.eling.elcms.system.model.CommonUser;
import com.eling.elcms.system.service.ICommonUserManager;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TravelAccountServiceImpl  extends GenericManagerImpl<TravelAccount,Long> implements ITravelAccountService{

	ITravelAccountDao TravelAccountDao;
	@Autowired
	IAccountManager accountManager;
	@Autowired
    IAccountFlowManager accountFlowManager;
	@Autowired
    IMemberManager memberManager;
	@Autowired
    ICommonUserManager commonUserManager;
	
	@Autowired
	public void setConsumerAccountDao(ITravelAccountDao TravelAccountDao) {
		this.dao = TravelAccountDao;
		this.TravelAccountDao = TravelAccountDao;
	}

	@Override
	public List<TravelAccount> queryCustomerAttendantInfo(String cardId) {
		TravelAccount travelAccount = new TravelAccount();
		PropertyUtils.setProperty(travelAccount, "roomPersonalInfo.consumeCard.cardNumber", cardId);
		List<TravelAccount> customerAccounts = query(travelAccount);
		if(customerAccounts == null || customerAccounts.isEmpty()){
			PropertyUtils.setProperty(travelAccount, "roomPersonalInfo", null);
			PropertyUtils.setProperty(travelAccount, "attendant.cno", cardId);
			customerAccounts = query(travelAccount);
		}
		return customerAccounts;
	}

	@Override
	public List<Account> queryCustomerInfoByCard(String cardId) {
		List<Account> list = new ArrayList<>();
		if(cardId != null && !cardId.equals("")){
			Account temp = new Account();
			PropertyUtils.setProperty(temp, "member.card.chipNumber", cardId);
			PropertyUtils.setProperty(temp, "validatePrivilege", false);
			temp.setAccountStatus(Account.AccountStatus.Effective);
			temp.setAccountType(Account.AccountType.Common);
			list.addAll(accountManager.query(temp));
		}
		return list;
	}

	@Override
	public AccountFlow payByCard(AccountFlow accountFlow,String token) {
        Account account = new Account();
	    if(accountFlow.getAccount().getPkAccount() != null){
            account = accountManager.get(accountFlow.getAccount().getPkAccount());
            account.setBalance(account.getBalance() - accountFlow.getMoney());
            account = accountManager.save(account);
            accountFlow.setPaymentMethod(PaymentMethod.ExpenseCard);//消费卡
        }else {
            Member member = memberManager.get(accountFlow.getAccount().getMember().getPkMember());
            account.setMember(member);
            PropertyUtils.setProperty(account, "validatePrivilege", false);
            account.setAccountStatus(Account.AccountStatus.Effective);
            account.setAccountType(Account.AccountType.Common);
            account = accountManager.query(account).get(0);
            accountFlow.setPaymentMethod(PaymentMethod.Cash);//现金
        }
        if(StringUtils.isNotEmpty(token)){
            ElingToken elingToken = ElingTokenUtils.decodeToken(token);
            String[] split = elingToken.getUserCode().split("@@@");
            CommonUser commonUser = new CommonUser();
            commonUser.setCode(split[0]);
            List<CommonUser> list = commonUserManager.query(commonUser);
            accountFlow.setOperator(list.get(0));
        }
        accountFlow.setAccount(account);
        accountFlow.setBalance(account.getBalance());
        accountFlow.setOperateType(AccountFlow.OperateType.XiRuanPay);
        accountFlow.setChargeType("餐饮");
        accountFlow.setExternalOperator("XiRuan");
        accountFlow.setOperateTime(new Date());

        accountFlow.setPayStatus(AccountFlow.PayStatus.Paid);
        accountFlow.setVersion(0);
        accountFlow = accountFlowManager.save(accountFlow);
        return accountFlow;
	}

    @Override
    public List<Account> queryCustomerInfoById(String idNumber) {
        List<Account> list = new ArrayList<>();
        Member member = new Member();
        PropertyUtils.setProperty(member,"personalInfo.idNumber",idNumber);
        List<Member> members = memberManager.query(member);
        if(CollectionUtils.isEmpty(members)){
            return null;
        }
        member = members.get(0);
        Account account = new Account();
        account.setMember(member);
        PropertyUtils.setProperty(account, "validatePrivilege", false);
        account.setAccountStatus(Account.AccountStatus.Effective);
        account.setAccountType(Account.AccountType.Common);
        list = accountManager.query(account);
        return list;
    }

    @Override
    public AccountFlow payBedByCard(AccountFlow accountFlow,String token) {
        Account account = new Account();
        if(accountFlow.getAccount().getPkAccount() != null){
            account = accountManager.get(accountFlow.getAccount().getPkAccount());
            account.setBalance(account.getBalance() - accountFlow.getMoney());
            account = accountManager.save(account);
            accountFlow.setPaymentMethod(PaymentMethod.ExpenseCard);//消费卡
        }else {
            Member member = memberManager.get(accountFlow.getAccount().getMember().getPkMember());
            account.setMember(member);
            PropertyUtils.setProperty(account, "validatePrivilege", false);
            account.setAccountStatus(Account.AccountStatus.Effective);
            account.setAccountType(Account.AccountType.Common);
            account = accountManager.query(account).get(0);
            accountFlow.setPaymentMethod(PaymentMethod.Cash);//现金
        }
        if(StringUtils.isNotEmpty(token)){
            ElingToken elingToken = ElingTokenUtils.decodeToken(token);
            String[] split = elingToken.getUserCode().split("@@@");
            CommonUser commonUser = new CommonUser();
            commonUser.setCode(split[0]);
            List<CommonUser> list = commonUserManager.query(commonUser);
            accountFlow.setOperator(list.get(0));
        }
        accountFlow.setAccount(account);
        accountFlow.setBalance(account.getBalance());
        accountFlow.setOperateType(AccountFlow.OperateType.XiRuanPay);
        accountFlow.setChargeType("床位");
        accountFlow.setExternalOperator("XiRuan");
        accountFlow.setOperateTime(new Date());

        accountFlow.setPayStatus(AccountFlow.PayStatus.Paid);
        accountFlow.setVersion(0);
        accountFlow = accountFlowManager.save(accountFlow);
        return accountFlow;
    }

}
